package com.yskj.otms.services.impl;

import com.yskj.otms.dao.IUserDao;
import com.yskj.otms.dto.ParamDto;
import com.yskj.otms.dto.ShipOwnerDto;
import com.yskj.otms.services.IUserServices;
import com.yskj.otms.utils.JsonUtil;
import com.yskj.otms.utils.SMSUtil;
import com.yskj.otms.utils.StringUtil;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by Administrator on 2019/1/8.
 */
@Service
public class UserServicesImpl implements IUserServices {
    private static Logger logger = LoggerFactory.getLogger(UserServicesImpl.class);
    @Autowired
    private IUserDao iUserDao;
    @Override
    public JSONObject toLogin(HttpServletRequest request) throws Exception {
        JSONObject obj = JsonUtil.getJSONObject(request);
        logger.info("UserServicesImpl toLogin start obj{}",obj);
        String phoneNum = JsonUtil.getJSONObjectString(obj,"phoneNum");
        String password = JsonUtil.getJSONObjectString(obj,"password");
        JSONObject jsonObject = new JSONObject();
        if (StringUtil.isNotEmpty(phoneNum) && StringUtil.isNotEmpty(password)){
            ParamDto paramDto = new ParamDto();
            paramDto.setPhoneNum(phoneNum);
            paramDto.setPwd(password);
            ShipOwnerDto shipOwnerDto = iUserDao.selectShipOwnerByPhoneNumAndPwd(paramDto);
            if (shipOwnerDto ==null){
                jsonObject.put("SUCCESS",false);
                jsonObject.put("MSG","输入的账号密码不正确！");
                return jsonObject;
            }
            HttpSession session = request.getSession();
            session.setAttribute("SESSION_SHIPOWNER_ID",shipOwnerDto.getId());
            session.setAttribute("SESSION_PHONE_NUM",shipOwnerDto.getPhoneNum());
            jsonObject.put("SUCCESS",true);
            jsonObject.put("MSG","恭喜您，登录成功！");
        }else{
            jsonObject.put("SUCCESS",false);
            jsonObject.put("MSG","账号密码不能为空！");
        }
        return jsonObject;
    }

    @Override
    public JSONObject getVerificationCode(HttpServletRequest request) throws Exception {
        JSONObject obj = JsonUtil.getJSONObject(request);
        logger.info("UserServicesImpl getVerificationCode start obj{}",obj);
        String phoneNum = JsonUtil.getJSONObjectString(obj,"phoneNum");
        ParamDto paramDto = new ParamDto();
        JSONObject jsonObject = new JSONObject();
        JSONObject retObj = new JSONObject();
        if (StringUtil.isNotEmpty(phoneNum)){
            paramDto.setPhoneNum(phoneNum);
            ShipOwnerDto shipOwnerDto = iUserDao.selectShipOwnerByPhoneNum(paramDto);
            if (shipOwnerDto == null){
                jsonObject.put("SUCCESS",false);
                jsonObject.put("MSG","该手机号码不存在！");
                return jsonObject;
            }
            String verificationCode = StringUtil.getRandomNumber(4);
            retObj = sendSmS(phoneNum,"SMS_151515175",verificationCode);
            if (retObj.getBoolean("SUCCESS")) {
                paramDto.setPwd(verificationCode);
                iUserDao.updateShipOwnerPwdByPhoneNum(paramDto);
            }else{
                return retObj;
            }
        }else{
            jsonObject.put("SUCCESS",false);
            jsonObject.put("MSG","手机号码不能为空！");
            return jsonObject;
        }
        jsonObject.put("SUCCESS",true);
        return jsonObject;
    }

    private JSONObject sendSmS(String phoneNum, String templateCode, String verification) {
        JSONObject obj = new JSONObject();
        obj.put("phoneNumbers", phoneNum);
        obj.put("signName", "汇找船");
        obj.put("templateCode", templateCode);
        obj.put("templateParam", "{\"code\":" + verification + "}");
        logger.info("LoginServiceImpl sendVerification phoneNum:{},verification:{}", phoneNum, verification);
        obj = SMSUtil.SendEmail(obj);
        System.out.println("obj : " + obj);
        JSONObject retObj = new JSONObject();
        if ("N".equals(obj.getString("OK"))) {
            retObj.put("SUCCESS", false);
            retObj.put("MSG", "短信发送失败！");
            return retObj;
        }
        retObj.put("SUCCESS", true);
        retObj.put("MSG", "短信发送成功！");
        return retObj;
    }
}
